﻿using System.Collections.Generic;
using System.Linq;
using DroidClaim.DAL.Entities;
using NHibernate;
using NHibernate.Criterion;
using System;
using DroidClaim.DAL.Models;


namespace DroidClaim.DAL.Dal
{
    public class SurveyorDAL
    {
        private readonly ISession _session;
        public SurveyorDAL(ISession session)
        {
             this._session = session;
        }

        public List<Surveyor> GetAll(SurveyorSearch searchCriteria)
        {
            var criteria = _session.CreateCriteria(typeof(Surveyor));

            if (searchCriteria != null)
            {
                // Search	
				if (!string.IsNullOrEmpty(searchCriteria.SurveyerId))
				{
					criteria.Add(Expression.Like("SurveyerId", searchCriteria.SurveyerId, MatchMode.Start));
				}
						
															
				if (!string.IsNullOrEmpty(searchCriteria.Username))
				{
					criteria.Add(Expression.Like("Username", searchCriteria.Username, MatchMode.Start));
				}
						
															
				if (!string.IsNullOrEmpty(searchCriteria.Name))
				{
					criteria.Add(Expression.Like("Name", searchCriteria.Name, MatchMode.Start));
				}
						
															
				if (!string.IsNullOrEmpty(searchCriteria.Code))
				{
					criteria.Add(Expression.Like("Code", searchCriteria.Code, MatchMode.Start));
				}

                //Sort
                if (!string.IsNullOrEmpty(searchCriteria.Sort))
                {
                    if (!string.IsNullOrEmpty(searchCriteria.SortType) && searchCriteria.SortType.ToLower() == "desc")
                    {
                        criteria.AddOrder(Order.Desc(searchCriteria.Sort));
                    }
                    else
                    {
                        criteria.AddOrder(Order.Asc(searchCriteria.Sort));
                    }
                }
            }

            List<Surveyor> result = criteria.List<Surveyor>().ToList();
            if (result == null || result.Count <= 0)
            {
                return result;
            }

            if (searchCriteria.SurveyorLocation != null)
            {
                List<Surveyor> surveyorList = result.FindAll(a => a.SurveyorLocation != null);
                result = surveyorList.FindAll(a => a.SurveyorLocation.Status == searchCriteria.SurveyorLocation.Status);
            }

            return criteria.List<Surveyor>().ToList();
        }

        public void Add(Surveyor surveyor)
        {
            _session.Save( surveyor);
        }

        public void Update(Surveyor surveyor)
        {
			 surveyor = _session.Merge(surveyor);
            _session.Update( surveyor);
        }

        public void Delete(List<string> idList)
        {
            int row = _session.CreateQuery("DELETE Surveyor where Id in (:IdList)").SetParameterList("IdList", idList).ExecuteUpdate();
            if (row != idList.Count)
                throw new Exception("Delete Not Success");

        }
    }
}

